package com.twinkle.bank;

import java.util.Scanner;

/**
 * ATM
 */
public class ATM {
    private Bank bank;

    public ATM() {
        bank = Bank.getInstance();
    }

    public void service() {
        Scanner sc = new Scanner(System.in);
        System.out.println("1.插卡");
        System.out.println("2.退出");
        System.out.println("请选择操作:");
        String firstChoice = sc.nextLine();
        switch (firstChoice) {
            case "1" -> {
                System.out.print("输入卡号:");
                String cardNo = sc.nextLine();
                System.out.print("输入密码:");
                String password = sc.nextLine();
                Account account = bank.verify(cardNo, password);
                if (account != null) {//账号和密码都对
                    boolean isExit = false;
                    do {
                        System.out.println("1.存钱");
                        System.out.println("2.取钱");
                        System.out.println("3.还贷");
                        System.out.println("4.查看余额");
                        System.out.println("5.退出");
                        System.out.println("请选择操作:");
                        String secondChoice = sc.nextLine();
                        switch (secondChoice) {
                            case "1":
                                System.out.print("输入存款金额：");
                                int saveMoney = Integer.parseInt(sc.nextLine());
                                bank.saveMoney(account, saveMoney);
                                break;
                            case "2":
                                System.out.print("输入取款金额：");
                                int withdrawMoney = Integer.parseInt(sc.nextLine());
                                try {
                                    bank.withdrawMoney(account, withdrawMoney);
                                } catch (NotEnoughMoneyException e) {
                                    System.out.println("出错了:" + e.getMessage());
                                }
                                break;
                            case "3":
                                if (account instanceof IRepay) {
                                    System.out.print("输入还贷金额：");
                                    int loanMoney = Integer.parseInt(sc.nextLine());
                                    bank.loan((IRepay) account, loanMoney);
                                } else {
                                    System.out.println("当前卡未开通还贷功能,请至柜台办理");
                                }
                                break;
                            case "4":
                                System.out.println("当前余额:" + bank.showBalance(account));
                                break;
                            case "5":
                                isExit = true;
                                break;
                            default:
                                System.out.println("输入错误");
                                break;
                        }
                    } while (!isExit);
                } else {
                    System.out.println("卡号或者密码错误");
                }
            }
            case "2" -> System.out.println("谢谢使用");
            default -> System.out.println("输入错误");
        }
        sc.close();
    }
}